package com.ghck.shop.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ghck.server.exception.ErrorConstant;
import com.ghck.server.exception.GhckException;
import com.ghck.shop.dao.SellerMapper;
import com.ghck.shop.model.Seller;
import com.ghck.shop.service.SellerService;
import com.ghck.shop.utils.FileStoreUtil;

@Service
@Transactional
public class SellerServiceImpl implements SellerService{

	@Autowired
	private SellerMapper sellerMapper;
	
	
	@Override
	public Integer addSeller(Seller seller) {
		Seller dbSeller  = sellerMapper.findSellerByUserId(seller.getUserId());
		if(dbSeller != null ){
			if(dbSeller.getStatus() != 2){
				GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"您已经申请商家了,请等待审核");
				throw exception;
			}
		}
		
		Date date = new Date();
		seller.setCreateTime(date);
		seller.setUpdateTime(date);
		seller.setStatus(0);
		return sellerMapper.addSeller(seller);
	}

	@Override
	public List<Seller> listAllSeller(Seller seller) {
		return null;
	}

	@Override
	public void sellerPass(Seller seller) {
		Seller dbSeller = sellerMapper.findSellerById(seller.getId());
		if(dbSeller == null){
			GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"该商家申请不存在");
			throw exception;
		}else {
			if(1 == dbSeller.getStatus()){
				GhckException exception = new GhckException(ErrorConstant.OPERATION_ERROR.getErrorCode(),"该商家已经审核过了");
				throw exception;
			}
		}
		
	}

	@Override
	public void sellerReject(Seller seller) {
		sellerMapper.updateSellerStatus(seller.getId(), 2);
	}

	@Override
	public List<Seller> listSellers(Seller seller, Integer start, Integer limit) {
		List<Seller> list = sellerMapper.listSellers(seller, start, limit);
		if(CollectionUtils.isNotEmpty(list)){
			for(Seller item:list){
				if(StringUtils.isNotEmpty(item.getCardFront())){
					item.setCardFront(FileStoreUtil.getBaseUrl() + item.getCardFront());
				}
				if(StringUtils.isNotEmpty(item.getCardBack())){
					item.setCardBack(FileStoreUtil.getBaseUrl() + item.getCardBack());
				}
				if(StringUtils.isNotEmpty(item.getBusinessLicence())){
					item.setBusinessLicence(FileStoreUtil.getBaseUrl() + item.getBusinessLicence());
				}
			}
		}
		return list;
	}
	@Override
	public Integer countListSellers(Seller seller) {
		return sellerMapper.countListSellers(seller);
	}

	@Override
	public Seller findSellerByUsrId(Integer userId) {
		return sellerMapper.findSellerByUserId(userId);
	}

	@Override
	public Seller findSellerById(Integer id) {
		return sellerMapper.findSellerById(id);
	}

	@Override
	public Integer updateSellerStatus(Integer id, Integer status) {
		return sellerMapper.updateSellerStatus(id, status);
	}

	

}
